package org.ccay.excel.meta;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.ccay.core.exception.ApplicationException;
import org.ccay.excel.exception.ExcelApplicationException;

/**
 * Excel文件的元数据信息
 * 	
 * @author chaowangbang
 * @since Mar 16, 2012
 */
public class ExcelMeta implements java.lang.Cloneable{
	
	/**
	 * 类型标识
	 */
	private String type;
	
	/**
	 * 最大允许的错误数
	 */
	private int maxAllowedErrors = Integer.MAX_VALUE;
	
	/**
	 * 错误文件配置
	 */
	private String errorDisplay;
	
	/**
	 * 权限点
	 */
	private String privilege;
	
	/**
	 * 储存Excel包含的sheet元数据
	 */
	private List<SheetMeta> sheets = new ArrayList<SheetMeta>();
	
	/**
	 * 添加Column元数据异常
	 */
	private static final String ADDSHEET_EXCEPTION = "ccay.excel.00080003";
	
	/**
	 * 文件名称
	 */
	private String fileName;
	
	/**
	 * 动态变换提供者
	 */
	private String dynamicProvider;
	
	private String noTransaction;
	
	public String getNoTransaction() {
		return noTransaction;
	}

	public void setNoTransaction(String noTransaction) {
		this.noTransaction = noTransaction;
	}

	public String getErrorDisplay() {
		return errorDisplay;
	}

	public void setErrorDisplay(String errorDisplay) {
		this.errorDisplay = errorDisplay;
	}

	/**
	 * @param type
	 */
	public ExcelMeta(String type) {
		this.type = type;
	}
	
	/** 获取指定名称的Sheet	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @param sheetName
	 * @return
	 */
	public SheetMeta getSheetMeta(String sheetName) {
		Iterator<SheetMeta> iter = sheets.iterator();
		while(iter.hasNext()) {
			SheetMeta sheet = iter.next();
			if(sheet.getSheetName().equals(sheetName)) {
				return sheet;
			}
		}
		
		return null;
	}
	/** 获取指定名称的Sheet	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @param sheetName
	 * @return
	 */
	public SheetMeta getSheetMetaByDispalyName(String dispalyName) {
		Iterator<SheetMeta> iter = sheets.iterator();
		while(iter.hasNext()) {
			SheetMeta sheet = iter.next();
			if(sheet.getLanguageNames().containsValue(dispalyName)) {
				return sheet;
			}
		}
		
		return null;
	}
	
	/** 添加一个SheetMeta	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @param sheetMeta
	 * @throws ApplicationException 
	 */
	public void addSheetMeta(SheetMeta sheetMeta) throws ApplicationException {
		//判断是否有重复的sheet
		for(SheetMeta sheet:this.sheets){
			if(sheet.equals(sheetMeta)){
				throw new ExcelApplicationException(ADDSHEET_EXCEPTION,sheet.getSheetName());
			}			
		}
		this.sheets.add(sheetMeta);
	}
	
	/** 获取Sheet集合
	 * @return the sheetMap
	 */
	public List<SheetMeta> getSheetList() {
		return sheets;
	}
	
	/** 获取权限点	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @return
	 */
	public String getPrivilege() {
		return privilege;
	}
	
	/**设置权限点
	 * @param privilege the privilege to set
	 */
	public void setPrivilege(String privilege) {
		this.privilege = privilege;
	}
	
	/** 获取type	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @return
	 */
	public String getType() {
		return this.type;
	}
	
	/** 设置最大允许的错误数	
	 * @author chaowangbang
	 * @since Mar 20, 2012
	 * @param maxAllowedErrors
	 */
	public void setMaxAllowedErrors(int maxAllowedErrors) {
		this.maxAllowedErrors = maxAllowedErrors;
	}

	/**
	 * @return the maxAllowedErrors
	 */
	public int getMaxAllowedErrors() {
		return maxAllowedErrors;
	}
	
	/** 获取指定名称的sheet、column中的验证器配置。	
	 * @author chaowangbang
	 * @since Mar 20, 2012
	 * @param sheetName
	 * @param colName
	 * @return
	 * @throws ExcelApplicationException
	 */
	@SuppressWarnings("unchecked")
	public List<ValidatorMeta> getValidatorMetas(String sheetName, String colName) {
		SheetMeta sheetMeta = this.getSheetMeta(sheetName);
		if(sheetMeta == null) {
			return Collections.EMPTY_LIST;
		}
		
		ColumnMeta columnMeta = sheetMeta.getColumnByName(colName);
		if(columnMeta == null) {
			return Collections.EMPTY_LIST;
		}
		
		return columnMeta.getValidators();
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public ExcelMeta copy() throws CloneNotSupportedException{
		ExcelMeta copyExcelMata = (ExcelMeta) this.clone();
		List<SheetMeta> copySheets = new ArrayList<SheetMeta>();
		for(SheetMeta meta:this.getSheetList()){
			copySheets.add(meta.copy());
		}
		copyExcelMata.sheets = copySheets;
		return copyExcelMata;
	}

	public String getDynamicProvider() {
		return dynamicProvider;
	}

	public void setDynamicProvider(String dynamicProvider) {
		this.dynamicProvider = dynamicProvider;
	}

}
